using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using WindowsLive.Writer.Api;

namespace InsertTranslation
{
    public partial class frmMain : Form
    {
        string[] transcodes = { "ar|en", "zh|en", "zh-CN|zh-TW", "zh-TW|zh-CN", "en|ar", "en|zh-CN", "en|zh-TW", "en|fr", "en|de", "en|it", "en|ja", "en|ko", "en|pt", "en|ru", "en|es", "fr|en", "fr|de", "de|en", "de|fr", "it|en", "ja|en", "ko|en", "pt|en", "ru|en", "es|en" };
        string[] transnames = { "Arabic to English BETA", "Chinese to English BETA", "Chinese (Simplified to Traditional) BETA", "Chinese (Traditional to Simplified) BETA", "English to Arabic BETA", "English to Chinese (Simplified) BETA", "English to Chinese (Traditional) BETA", "English to French", "English to German", "English to Italian", "English to Japanese BETA", "English to Korean BETA", "English to Portuguese", "English to Russian BETA", "English to Spanish", "French to English", "French to German", "German to English", "German to French", "Italian to English", "Japanese to English BETA", "Korean to English BETA", "Portuguese to English", "Russian to English BETA", "Spanish to English" };
        string translate = "";
        public string transresult = "";

        public frmMain(string text)
        {
            InitializeComponent();
            txbxTextToTranslate.Text = text;
            LoadCodes();
        }

        private void LoadCodes()
        {
            foreach (string s in transnames)
            {
                cbCodes.Items.Add(s);
            }
            cbCodes.SelectedIndex = 0;
        }

        private void btnTranslate_Click(object sender, EventArgs e)
        {
            translate = transcodes[cbCodes.SelectedIndex];
            
            if (txbxTextToTranslate.Text != "")
            {
                transresult = TranslateText(txbxTextToTranslate.Text, translate);
                txbxTranslatedText.Text = transresult;
            }
            else
                MessageBox.Show("No text has been entered for translation", "Big Fat Hairy Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
        }

        public string TranslateText(string input, string languagePair)
        {
            System.IO.Stream st = null;
            System.IO.StreamReader sr = null;
            string result = "";
            try
            {
                string url = String.Format("http://www.google.com/translate_t?hl=en&ie=UTF8&text={0}&langpair={1}", input, languagePair);
                // make a Web request
                PluginHttpRequest req = new PluginHttpRequest(url);

                st = req.GetResponse();
                sr = new System.IO.StreamReader(st);

                // read all the text in it
                result = sr.ReadToEnd();
                result = result.Substring(result.IndexOf("id=result_box") + 22, result.IndexOf("id=result_box") + 500);
                result = result.Substring(0, result.IndexOf("</div"));
            }
            catch (Exception ex)
            {
                MessageBox.Show("Failed to translate for the following reason: " + ex.Message, "Big Fat Hairy Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            finally
            {
                // always close readers and streams
                sr.Close();
                st.Close();
            }
            return result;
        }

        private void btnInsert_Click(object sender, EventArgs e)
        {
            if (txbxTranslatedText.Text != "")
            {
                this.DialogResult = DialogResult.OK;
                this.Close();
            }
            else
                MessageBox.Show("No translated text to insert", "Big Fat Hairy Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
        }
        

    }
}